package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.KindExample;
import com.campusify.pojo.KindExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.KindMapper;
import com.campusify.pojo.Kind;
import com.campusify.service.KindService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class KindServiceImpl implements KindService {

	@Autowired
	private KindMapper kindMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Kind> findAll() {
		return kindMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Kind> page=   (Page<Kind>) kindMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Kind kind) {
		kindMapper.insert(kind);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Kind kind){
		kindMapper.updateByPrimaryKey(kind);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Kind findOne(Integer kindID){
		return kindMapper.selectByPrimaryKey(kindID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] kindIDs) {
		for(Integer kindID:kindIDs){
			kindMapper.deleteByPrimaryKey(kindID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Kind kind, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		KindExample example=new KindExample();
		Criteria criteria = example.createCriteria();
		
		if(kind!=null){			
						if(kind.getKindname()!=null && kind.getKindname().length()>0){
				criteria.andKindnameLike("%"+kind.getKindname()+"%");
			}	
		}
		
		Page<Kind> page= (Page<Kind>)kindMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
